package com.qifan.energy.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.qifan.energy.R;
import com.qifan.energy.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CategoryFragment extends BaseFragment {

    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    private List<Fragment> pageList = new ArrayList<Fragment>() {{
        add(CategoryNewFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());
        /*add(CategoryRecommendFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());
        add(CategoryRecommendFragment.newInstance());*/
    }};
    private List<String> titles = new ArrayList() {{
        add("新品上架");
        add("奶粉辅食");
        add("童装童鞋");
        add("童车童床");
        /*add("喂养用品");
        add("玩具早教");
        add("益智玩具");
        add("妈咪专区");*/
    }};

    public static CategoryFragment newInstance() {
        return new CategoryFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    protected void initView(View view) {
        viewPager.setAdapter(new FragmentPagerAdapter(getParentFragmentManager(),0) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return pageList.get(position);
            }

            @Override
            public int getCount() {
                return pageList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });
        //预加载页面个数
        viewPager.setOffscreenPageLimit(pageList.size()/2);
        //关联ViewPager
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void initData() {
        //注册EventBus
        //EventBusUtils.register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //注销EventBus
        //EventBusUtils.unregister(this);
    }

    /**
     * 接收到分发的事件
     * @param event 事件
     */
    /*@Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventMessage event) {
        if (event.getCode() == EventCode.EVENT_1) {
            //cartList();
        }
    }*/
}
